﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GoDinner.Models;

namespace GoDinner.Controllers
{    
    public class GerechtenController : Controller
    {
        Models.GoDinnerDbContext _db = new Models.GoDinnerDbContext();

        [Authorize(Roles="Admin")]
        public ActionResult OverzichtGerechten()
        {
            ViewBag.Message = "Overzicht gerechten";
            var model = _db.GerechtTypes.ToList();
            
            return View(model);
        }

        [HttpGet]
        [Authorize(Roles = "Admin")]
        public ActionResult CreateGerecht()
        {
            return View();
        }

        [HttpPost]
        [Authorize(Roles = "Admin")]
        public ActionResult CreateGerecht(Gerecht gerecht)
        {
            if (ModelState.IsValid)
            {
                _db.Gerechten.Add(gerecht);
                _db.SaveChanges();
                return RedirectToAction("OverzichtGerechten","Gerechten");
            }
            return View(gerecht);
        }

        [HttpGet]
        [Authorize(Roles = "Admin")]
        public ActionResult EditGerecht(int id)
        {
            var model = _db.Gerechten.Find(id);
            return View(model);
        }

        [HttpPost]
        [Authorize(Roles = "Admin")]
        public ActionResult EditGerecht(Gerecht gerecht)
        {
            if (ModelState.IsValid)
            {
                _db.Entry(gerecht).State = EntityState.Modified;
                _db.SaveChanges();
                return RedirectToAction("OverzichtGerechten", "Gerechten");
            }
            return View(gerecht);
        }

        [HttpGet]
        [Authorize(Roles = "Admin")]
        public ActionResult DeleteGerecht(int id)
        {
            Gerecht gerecht = _db.Gerechten.Find(id);

            if (ModelState.IsValid)
            {
                _db.Entry(gerecht).State = EntityState.Deleted;
                _db.SaveChanges();
                return RedirectToAction("OverzichtGebruiker", "Gebruiker");
            }
            return View(gerecht);

        }

    }
}
